package com.example.administrator.seppdzxing.utils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.administrator.seppdzxing.ICaptureView;
import com.example.administrator.seppdzxing.R;
import com.example.administrator.seppdzxing.camera.CameraManager;
import com.example.administrator.seppdzxing.decode.DecodeThread;
import com.google.zxing.Result;



public class CaptureActivityHandler extends Handler {

	private final ICaptureView captureView;
	private final DecodeThread decodeThread;
	private CameraManager cameraManager;
	private State state;
	public static boolean start = true;
	private enum State {
		PREVIEW, SUCCESS, DONE
	}
	public CaptureActivityHandler(ICaptureView captureView, CameraManager cameraManager, int decodeMode) {
		this.captureView = captureView;
		decodeThread = new DecodeThread(captureView, decodeMode);
		decodeThread.start();
		state = State.SUCCESS;
		this.cameraManager = cameraManager;
		if(cameraManager!=null){
			cameraManager.startPreview();
		}

		restartPreviewAndDecode();
	}

	public void setCameraManager(CameraManager cameraManager){
		this.cameraManager = cameraManager;
		state = State.SUCCESS;
		cameraManager.startPreview();
		restartPreviewAndDecode();
	}

	@Override
	public void handleMessage(Message message) {
		if (message.what == R.id.restart_preview) {
			restartPreviewAndDecode();
		} else if (message.what == R.id.decode_succeeded) {
			state = State.SUCCESS;
			Bundle bundle = message.getData();
			captureView.handleDecode((Result) message.obj, bundle);
		} else if (message.what == R.id.decode_failed) {// We're decoding as fast as possible, so when one decode fails,
			// start another.
			state = State.PREVIEW;
			cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
		} else if(message.what == R.id.decode_bitmap_failed){
			state = State.PREVIEW;
			cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
			captureView.handleDecodeBitmapError();
		}else if(message.what == R.id.stop_sport){
			state = State.SUCCESS;
		}
	}

	public void quitSynchronously() {
		state = State.DONE;
		cameraManager.stopPreview();
		Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
		quit.sendToTarget();
		try {
			// Wait at most half a second; should be enough time, and onPause()
			// will timeout quickly
			decodeThread.join(500L);
		} catch (InterruptedException e) {
			// continue
		}
		removeMessages(R.id.decode_succeeded);
		removeMessages(R.id.decode_failed);
		removeMessages(R.id.restart_preview);
		removeMessages(R.id.decode_bitmap_failed);
	}

	public void stopeStoprt() {
		try {
			cameraManager.stopSport();
			sendEmptyMessage(R.id.stop_sport);
		}catch (Exception e){
		}

	}

	public void starStoprt(){
		cameraManager.startSoprt();
		sendEmptyMessageDelayed(R.id.restart_preview, 100);
//		Message quit = Message.obtain(this, R.id.restart_preview);
//		quit.sendToTarget();
	}

	public void decodeBitmap(String picturePath) {
		stopeStoprt();
		Message quit = Message.obtain(decodeThread.getHandler(), R.id.decode_bitmap,picturePath);
		quit.sendToTarget();
	}

	private void restartPreviewAndDecode() {
		if (state == State.SUCCESS) {
			state = State.PREVIEW;
			cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
		}
	}

}
